#include "defines.h"
#include "comm.h"
#include "draw_staff.h"

void graph_startup(const char *y, const char *u, const char *v)
{
	char buf[20];

	command_jmpi(1, L_DRAW_POINT_END);

	add_comment("a sub-routine to draw a dot");
	add_label(L_DRAW_POINT);

// Set Position Y
	command_shl(1, REG_M_POSX, REG_POS_X, inum2str(buf, 2, ud));	// Block X position
	command_shl(1, REG_M_POSY, REG_POS_Y, inum2str(buf, 2, ud));	// Block Y position
// Set Block Size Y
	command_mov(1, REG_M_BLOCKSIZE, BLOCK_SIZE_Y);	// Block height & width (4*4 bytes)
// Set Color Y
	command_mov(8, REG_M_COLOR_UW, REG_COLOR_Y_UW);
// OK, Send Y
	macro_media_block_write(Y_SURFACE);

// Set Position UV
	command_shl(1, REG_M_POSY, REG_POS_Y, inum2str(buf, 1, ud));	// Block Y position
// Set Block Size UV
	command_mov(1, REG_M_BLOCKSIZE, BLOCK_SIZE_UV);	// Block height & width (4*2 bytes)
// Set Color UV
	command_mov(4, REG_M_COLOR_UW, REG_COLOR_UV_UW);
// OK, Send UV
	macro_media_block_write(UV_SURFACE);

	macro_return();

	add_label(L_DRAW_POINT_END);
	add_comment("clear the message registers");
	command_mov(8, REG_M_HEADER, ZERO);	// Clear m0
	command_mov(8, REG_M_BODY, ZERO);	// Clear m0
	graph_set_color(y, u, v);
}

void graph_set_color(const char *y, const char *u, const char *v)
{
	char buf[20];

	add_comment("set color");

	command_mov(1, REG_COLOR_Y_UW, y);
	command_shl(1, REG_COLOR_Y_UW, REG_COLOR_Y_UW, inum2str(buf, 8, uw));
	command_add(1, REG_COLOR_Y_UW, REG_COLOR_Y_UW, y);

	command_mov(1, REG_COLOR_UV_UW, v);
	command_shl(1, REG_COLOR_UV_UW, REG_COLOR_UV_UW, inum2str(buf, 8, uw));
	command_add(1, REG_COLOR_UV_UW, REG_COLOR_UV_UW, u);
}

void draw_point(const char *x, const char *y)
{
	add_comment("set position");
	command_mov(1, REG_POS_X, x);	// Block X position
	command_mov(1, REG_POS_Y, y);	// Block Y position

	push_linkage();
	macro_call(L_DRAW_POINT);
	pop_linkage();
}

/*
static void point(int x, int y)
{
	char buf1[20];
	char buf2[20];
	draw_point(inum2str(buf1, x, uw), inum2str(buf2, y, uw));
}

void draw_line(int x1, int y1, int x2, int y2)
{
	int dx = (x1 >= x2) ? (x1 - x2) : (x2 - x1);

	int dy = (y1 >= y2) ? (y1 - y2) : (y2 - y1);

	int p, x, y, inc;

	if ((x1 < x2 && y1 < y2) || (x2 < x1 && y2 < y1))

		inc = 1;
	else
		inc = -1;

	if (dx > dy)
	{
		p = 2 * dy - dx;
		if (x1 > x2)
		{
			x = x2;
			y = y2;
			x2 = x1;
		}
		else
		{
			x = x1;
			y = y1;
		}
		point(x, y);
		while (x < x2)
		{
			x++;
			if (p < 0)
				p += 2 * dy;
			else
			{
				y += inc;
				p += 2 * (dy - dx);
			}
			point(x, y);
		}
	}
	else
	{
		p = 2 * dx - dy;
		if (y1 > y2)
		{
			x = x2;
			y = y2;
			y2 = y1;
		}
		else
		{
			x = x1;
			y = y1;
		}
		point(x, y);
		while (y < y2)
		{
			y++;
			if (p < 0)
				p += 2 * dx;
			else
			{
				x += inc;
				p += 2 * (dx - dy);
			}
			point(x, y);
		}

	}
}

void draw_box(int x1, int y1, int x2, int y2)
{
	draw_line(x1, y1, x2, y1);
	draw_line(x1, y2, x2, y2);
	draw_line(x1, y1, x1, y2);
	draw_line(x2, y1, x2, y2);
}

static void circlePlotPoints(int cx, int cy, int x, int y)
{
	point(cx + x, cy + y);
	point(cx - x, cy + y);
	point(cx + x, cy - y);
	point(cx - x, cy - y);
	point(cx + y, cy + x);
	point(cx - y, cy + x);
	point(cx + y, cy - x);
	point(cx - y, cy - x);
}

void draw_circle(int cx, int cy, int r)
{
	int x = 0, y = r, p = 1 - r;

	circlePlotPoints(cx, cy, x, y);
	while (x < y)
	{
		x++;
		if (p < 0)
			p += 2 * x + 1;
		else
		{
			y--;
			p += 2 * (x - y) + 1;
		}
		circlePlotPoints(cx, cy, x, y);
	}

}

static void ellipsePlotPoints(int cx, int cy, int x, int y)
{
	point(cx + x, cy + y);
	point(cx - x, cy + y);
	point(cx + x, cy - y);
	point(cx - x, cy - y);
}

inline int cal_round(const float a)
{
	return (int) (a + 0.5);
}

void draw_ellipse(int cx, int cy, int rx, int ry)
{
	int rx2 = rx * rx;

	int ry2 = ry * ry;

	int tworx2 = 2 * rx2;

	int twory2 = 2 * ry2;

	int p;

	int x = 0;

	int y = ry;

	int px = 0;

	int py = tworx2 * y;

	ellipsePlotPoints(cx, cy, x, y);

	p = cal_round(ry2 - (rx2 * ry) + (0.25 * rx2));
	while (px < py)
	{
		x++;
		px += twory2;
		if (p < 0)
			p += ry2 + px;
		else
		{
			y--;
			py -= tworx2;
			p += ry2 + px - py;
		}
		ellipsePlotPoints(cx, cy, x, y);
	}
	p = cal_round(ry2 * (x + 0.5) * (x + 0.5) + rx2 * (y - 1) * (y - 1) - rx2 * ry2);
	while (y > 0)
	{
		y--;
		py -= tworx2;
		if (p > 0)
			p += rx2 - py;
		else
		{
			x++;
			px += twory2;
			p += rx2 + px - py;
		}
		ellipsePlotPoints(cx, cy, x, y);
	}
}
*/
